package com.vikutorika.dao.imp;

import com.vikutorika.dao.BasicDAO;
import com.vikutorika.dao.GroupTextMessageDAO;
import com.vikutorika.entity.GroupTextMessage;
import com.vikutorika.util.V_DBUtils;
import com.vikutorika.vo.SendMessageVo;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

public class GroupTextMessageDAOImp extends BasicDAO<GroupTextMessage> implements GroupTextMessageDAO {
    @Override
    public List<GroupTextMessage> getAll() {
        return null;
    }

    @Override
    public int totalRow() {
        Connection conn= null;
        String sql = "select count(1) from group_text_message;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.query(conn,sql, new ScalarHandler<>());
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }

    @Override
    public List<GroupTextMessage> getByGID(Integer gid) {
        Connection conn= null;
        List<GroupTextMessage> list;
        String sql = "select * from group_text_message where gid = ? ;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            list = queryRunner.query(conn,sql, new BeanListHandler<>(GroupTextMessage.class), gid);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
        return list;
    }

    @Override
    public List<GroupTextMessage> getByGID_Recent(Integer gid, Integer length) {
        Connection conn= null;
        List<GroupTextMessage> list;
        String sql = "select * from group_text_message where gid = ? order by  create_time desc limit ?;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            list = queryRunner.query(conn,sql, new BeanListHandler<>(GroupTextMessage.class), gid, length);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
        return list;
    }

    @Override
    public List<GroupTextMessage> getByGID_Unread(Integer gid, Timestamp time) {
        Connection conn= null;
        List<GroupTextMessage> list;
        String sql = "select * from group_text_message where gid = ? and create_time > ? order by  create_time desc;";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            list = queryRunner.query(conn,sql, new BeanListHandler<>(GroupTextMessage.class), gid, time);
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
        return list;
    }

    @Override
    public int insert(SendMessageVo vo) {
        Connection conn= null;
        String sql = "insert into group_text_message values(default,?,?,default,?)";
        try {
            conn = V_DBUtils.getConnection();
            QueryRunner queryRunner = new QueryRunner();
            return queryRunner.update(conn,sql,vo.getId(),vo.getUid(),vo.getContent());
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
        finally {
            V_DBUtils.close(conn);
        }
    }
}
